package com.bjpowernode.oa.web.action;

import com.bjpowernode.oa.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DeptListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取应用的根路径
        String contextPath = request.getContextPath();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        try {
            con= DBUtil.getConnection();
            String sql="select deptno as a,dname,loc from dept";
            pstmt=con.prepareStatement(sql);
            rs=pstmt.executeQuery();
            out.print("<!DOCTYPE html>");
            out.print("<html lang='en'>");
            out.print("<head>");
            out.print("    <meta charset='UTF-8'>");
            out.print("    <title>List Page</title>");
            out.print("</head>");
            out.print("<body>");
            out.print("<script type='text/javascript'>");
            out.print("        function del(deptno){");
            out.print("    var ok=window.confirm('亲，请确认删除');");
            out.print("    if(ok){");
            out.print("        document.location.href='"+contextPath+"/dept/delete?deptno='+deptno;");
            out.print("    }");
            out.print("}");
			out.print("</script>");
            out.print("<h1 align='center'>部门列表</h1>");
            out.print("<hr/>");
            out.print("<table border='1px' align='center' width='50%'>");
            out.print("    <tr>");
            out.print("        <th>序号</th>");
            out.print("        <th>部门编号</th>");
            out.print("        <th>部门名称</th>");
            out.print("        <th>操作</th>");
            out.print("    </tr>");
            int i=0;
            while (rs.next()){
                String deptno=rs.getString("a");
                String dname=rs.getString("dname");
                String loc=rs.getString("loc");
                out.print("    <tr>");
                out.print("        <td>"+(++i)+"</td>");
                out.print("        <td>"+deptno+"</td>");
                out.print("        <td>"+dname+"</td>");
                out.print("        <td>");
                out.print("            <a href='"+contextPath+"/dept/edit?deptno="+deptno+"'>修改</a>");
                out.print("            <a href='javascript:void(0)' onclick='del("+deptno+")'>删除</a>");
                out.print("            <a href='"+contextPath+"/dept/detail?deptno="+deptno+"'>详情</a>");
                out.print("        </td>");
                out.print("    </tr>");
            }
            out.print("</table>");
            out.print("<hr/>");
            out.print("<a href='"+contextPath+"/add.html'>新增部门</a>");
            out.print("</body>");
            out.print("</html>");
        } catch (SQLException exception) {
            exception.printStackTrace();
        }finally {
            DBUtil.close(con,pstmt,rs);
        }
    }
}
